package com.anpy.security.service.impl;

import com.anpy.security.entity.TRole;
import com.anpy.security.repository.TRoleRepository;
import com.anpy.security.service.TRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * u89d2u8272u670du52a1u5b9eu73b0u7c7b
 */
@Service
public class TRoleServiceImpl implements TRoleService {

    private final TRoleRepository roleRepository;

    @Autowired
    public TRoleServiceImpl(TRoleRepository roleRepository) {
        this.roleRepository = roleRepository;
    }

    /**
     * u521bu5efau89d2u8272
     *
     * @param role u89d2u8272u5bf9u8c61
     * @return u521bu5efau540eu7684u89d2u8272u5bf9u8c61
     */
    @Override
    @Transactional
    public TRole createRole(TRole role) {
        // u68c0u67e5u89d2u8272u540du79f0u662fu5426u5df2u5b58u5728
        if (roleRepository.existsByName(role.getName())) {
            throw new IllegalArgumentException("u89d2u8272u540du79f0u5df2u5b58u5728");
        }
        
        // u8bbeu7f6eu521bu5efau65f6u95f4u548cu9ed8u8ba4u72b6u6001
        role.setCreatedAt(LocalDateTime.now());
        if (role.getStatus() == null) {
            role.setStatus(true);
        }
        
        return roleRepository.save(role);
    }

    /**
     * u66f4u65b0u89d2u8272
     *
     * @param id   u89d2u8272ID
     * @param role u89d2u8272u5bf9u8c61
     * @return u66f4u65b0u540eu7684u89d2u8272u5bf9u8c61
     */
    @Override
    @Transactional
    public TRole updateRole(Integer id, TRole role) {
        TRole existingRole = getRoleById(id);
        
        // u68c0u67e5u89d2u8272u540du79f0u662fu5426u5df2u88abu5176u4ed6u89d2u8272u4f7fu7528
        if (!existingRole.getName().equals(role.getName()) && 
            roleRepository.existsByName(role.getName())) {
            throw new IllegalArgumentException("u89d2u8272u540du79f0u5df2u5b58u5728");
        }
        
        // u66f4u65b0u89d2u8272u4fe1u606fuff0cu4fddu7559u539fu521bu5efau65f6u95f4u548cID
        existingRole.setName(role.getName());
        existingRole.setDescription(role.getDescription());
        if (role.getStatus() != null) {
            existingRole.setStatus(role.getStatus());
        }
        
        return roleRepository.save(existingRole);
    }

    /**
     * u6839u636eIDu83b7u53d6u89d2u8272
     *
     * @param id u89d2u8272ID
     * @return u89d2u8272u5bf9u8c61
     */
    @Override
    public TRole getRoleById(Integer id) {
        return roleRepository.findById(id)
                .orElseThrow(() -> new IllegalArgumentException("u89d2u8272u4e0du5b58u5728: " + id));
    }

    /**
     * u83b7u53d6u6240u6709u89d2u8272
     *
     * @return u89d2u8272u5217u8868
     */
    @Override
    public List<TRole> getAllRoles() {
        return roleRepository.findAll();
    }

    /**
     * u83b7u53d6u6240u6709u542fu7528u7684u89d2u8272
     *
     * @return u542fu7528u7684u89d2u8272u5217u8868
     */
    @Override
    public List<TRole> getActiveRoles() {
        return roleRepository.findByStatus(true);
    }

    /**
     * u542fu7528/u7981u7528u89d2u8272
     *
     * @param id     u89d2u8272ID
     * @param status u72b6u6001
     * @return u66f4u65b0u540eu7684u89d2u8272u5bf9u8c61
     */
    @Override
    @Transactional
    public TRole updateRoleStatus(Integer id, Boolean status) {
        TRole role = getRoleById(id);
        role.setStatus(status);
        return roleRepository.save(role);
    }

    /**
     * u5220u9664u89d2u8272
     *
     * @param id u89d2u8272ID
     */
    @Override
    @Transactional
    public void deleteRole(Integer id) {
        // u68c0u67e5u89d2u8272u662fu5426u5b58u5728
        if (!roleRepository.existsById(id)) {
            throw new IllegalArgumentException("u89d2u8272u4e0du5b58u5728: " + id);
        }
        roleRepository.deleteById(id);
    }
}
